<?php
class class_ABMAgenda
{
	private $_db; 
	function __construct()
	{
		$this->config();
	}
	function method_insertAgenda($params, $error)
    {
        $dias = array("0", "0", "0", "0", "0", "0", "0");
        for ($i=0; $i<=6; $i++)
        {
        	if ($params[0][$i] != "")
        	{
        		$dias[$params[0][$i]-1] = "1";
        	}
        }
        $dias = implode("' , '", $dias);
        $this->_db->setQuery("
        	INSERT INTO agenda 
        	(cod_clinica,
        	dni_usuario, 
        	lu, 
        	ma, 
        	mi, 
        	ju, 
        	vi, 
        	sa, 
        	do, 
        	hora_desde, 
        	hora_hasta, 
        	fecha_desde, 
        	fecha_hasta, 
        	observaciones, 
        	tipo) 
        	VALUES 
        	('" . strtoupper($params[8]) . "',
        	'" . strtoupper($params[7]) . "', 
        	'" . strtoupper($dias) . "', 
        	'" . strtoupper($params[1]) . "', 
        	'" . strtoupper($params[2]) . "', 
        	'" . strtoupper($params[3]) . "', 
        	'" . strtoupper($params[4]) . "', 
        	'" . strtoupper($params[5]) . "', 
        	'" . strtoupper($params[6]) . "')
        	");
        $this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	
    	if ($params[6] == 'E')
    	{
    		$this->_db->setQuery("SELECT celular FROM usuarios WHERE dni_usuario = '" . $params[7] . "'");
	    	$this->_db->alter();
	    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
	    	$reg = $this->_db->loadResult();
	    	$cel = $reg[0]->celular; 
    		return owFunciones::CellSend($cel,$params[5]);
    	}
		return true;
    }
    function method_updateAgenda($params, $error)
    {
    	if (count($params) != 8) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 8 parameter; got " . count($params)); return $error; }
        
        $dias = array("lu = '0'", "ma = '0'", "mi = '0'", "ju = '0'", "vi = '0'", "sa = '0'", "do = '0'");
        for ($i=0; $i<=6; $i++)
        {
        	if ($params[1][$i] != "")
        	{
        		switch ($params[1][$i])
        		{
        			case '1': $dias[$params[1][$i]-1] = "lu = '1'"; break; 
        			case '2': $dias[$params[1][$i]-1] = "ma = '1'"; break; 
        			case '3': $dias[$params[1][$i]-1] = "mi = '1'"; break;
        			case '4': $dias[$params[1][$i]-1] = "ju = '1'"; break;
        			case '5': $dias[$params[1][$i]-1] = "vi = '1'"; break;
        			case '6': $dias[$params[1][$i]-1] = "sa = '1'"; break;
        			case '7': $dias[$params[1][$i]-1] = "do = '1'"; break;
        		}
        	}
        }
        $dias = implode(", ", $dias);
		$this->_db->setQuery("
		UPDATE agenda SET 
		" . $dias . ", 
		hora_desde = '" . $params[2] . "', 
		hora_hasta = '" . $params[3] . "', 
		fecha_desde = '" . $params[4] . "', 
		fecha_hasta = '" . $params[5] . "', 
		observaciones = '" . $params[6] . "', 
		tipo = '" . $params[7] . "' 
		WHERE cod_agenda = '" . $params[0] . "'");
        $this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		return true;
    }
    function method_getDataToAgenda($params, $error)
    { 
        $this->_db->setQuery("
        SELECT cod_agenda, 
        if (lu=1, 'caraffa/atendido.png', '') as lu, 
        if (ma=1, 'caraffa/atendido.png', '') as ma, 
        if (mi=1, 'caraffa/atendido.png', '') as mi, 
        if (ju=1, 'caraffa/atendido.png', '') as ju, 
        if (vi=1, 'caraffa/atendido.png', '') as vi, 
        if (sa=1, 'caraffa/atendido.png', '') as sa, 
        if (do=1, 'caraffa/atendido.png', '') as do, 
        if (SUBSTR(hora_desde, 1, 5)='00:00','',SUBSTR(hora_desde, 1, 5)) as hora_desde, 
        if (SUBSTR(hora_hasta, 1, 5)='00:00','',SUBSTR(hora_hasta, 1, 5)) as hora_hasta, 
        if ((DATE_FORMAT(fecha_desde, '%d/%m/%Y'))='00/00/0000', '', (DATE_FORMAT(fecha_desde, '%d/%m/%Y'))) as fecha_desde, 
        if ((DATE_FORMAT(fecha_hasta, '%d/%m/%Y'))='00/00/0000', '', (DATE_FORMAT(fecha_hasta, '%d/%m/%Y'))) as fecha_hasta, 
        tipo 
        FROM agenda 
        WHERE
        cod_clinica = '" . $params[0]->cod_clinica . "' 
        AND dni_usuario = '" . $params[0]->medico . "'
        ");
        $this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	$regs = $this->_db->loadResult();
    	$rows = array();
    	FOREACH ($regs as $reg)
    	{
    		$row = array();
    		array_push($row, $reg->cod_agenda);
    		array_push($row, $reg->lu);
    		array_push($row, $reg->ma);
    		array_push($row, $reg->mi);
    		array_push($row, $reg->ju);
    		array_push($row, $reg->vi);
    		array_push($row, $reg->sa);
			array_push($row, $reg->do);
    		array_push($row, $reg->hora_desde);
    		array_push($row, $reg->hora_hasta);
    		array_push($row, $reg->fecha_desde);
    		array_push($row, $reg->fecha_hasta);
    		array_push($row, $reg->tipo);
    		array_push($rows, $row);
    	}
		return $rows;
    }
	function method_getMedicos($params, $error)
    {
    	if ($params[0]->medicos)
        	$this->_db->setQuery("SELECT dni_usuario as value, CONCAT(apellido, ' - ', nombres) as label FROM usuarios WHERE cod_tipo_usuario = '3' ORDER BY label");
        else
        	$this->_db->setQuery("SELECT dni_usuario as value, CONCAT(apellido, ' - ', nombres) as label FROM usuarios WHERE dni_usuario = '" . $_SESSION['dni_usuario'] . "'");
        $this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	
    	return $this->_db->loadResult();
    }
    function method_modificarAgenda($params, $error)
    {
        $this->_db->setQuery("SELECT cod_agenda, if (lu=1, 'caraffa/atendido.png', '') as lu, if (ma=1, 'caraffa/atendido.png', '') as ma, if (mi=1, 'caraffa/atendido.png', '') as mi, if (ju=1, 'caraffa/atendido.png', '') as ju, if (vi=1, 'caraffa/atendido.png', '') as vi, if (sa=1, 'caraffa/atendido.png', '') as sa, if (do=1, 'caraffa/atendido.png', '') as do, if (SUBSTR(hora_desde, 1, 5)='00:00','',SUBSTR(hora_desde, 1, 5)) as hora_desde, if (SUBSTR(hora_hasta, 1, 5)='00:00','',SUBSTR(hora_hasta, 1, 5)) as hora_hasta, if ((DATE_FORMAT(fecha_desde, '%d/%m/%Y'))='00/00/0000', '', (DATE_FORMAT(fecha_desde, '%d/%m/%Y'))) as fecha_desde, if ((DATE_FORMAT(fecha_hasta, '%d/%m/%Y'))='00/00/0000', '', (DATE_FORMAT(fecha_hasta, '%d/%m/%Y'))) as fecha_hasta, observaciones, tipo FROM agenda WHERE dni_usuario = '" . $params[1] . "' AND cod_agenda = '" . $params[0] . "'");
        $this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	$result = "";
    	$reg = "";
    	$reg = $this->_db->loadResult();
    	$dias = array();
    	if ($reg[0]->lu == "caraffa/atendido.png") array_push($dias, "1"); else array_push($dias, ""); 
    	if ($reg[0]->ma == "caraffa/atendido.png") array_push($dias, "2"); else array_push($dias, "");
    	if ($reg[0]->mi == "caraffa/atendido.png") array_push($dias, "3"); else array_push($dias, "");
    	if ($reg[0]->ju == "caraffa/atendido.png") array_push($dias, "4"); else array_push($dias, "");
    	if ($reg[0]->vi == "caraffa/atendido.png") array_push($dias, "5"); else array_push($dias, "");
    	if ($reg[0]->sa == "caraffa/atendido.png") array_push($dias, "6"); else array_push($dias, "");
    	if ($reg[0]->do == "caraffa/atendido.png") array_push($dias, "7"); else array_push($dias, "");
    	$result->cod_agenda = $reg[0]->cod_agenda;
    	$result->dias = $dias;
    	$result->hD = $reg[0]->hora_desde;
    	$result->hH = $reg[0]->hora_hasta;
    	$result->fD = $reg[0]->fecha_desde;
    	$result->fH = $reg[0]->fecha_hasta;
    	$result->observaciones = $reg[0]->observaciones;
    	$result->tipo = $reg[0]->tipo;
    	return $result;
    }
    function method_deleteAgenda($params, $error)
    {
    	if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        
        $this->_db->setQuery("DELETE FROM agenda WHERE cod_agenda = '" . $params[0] . "'");
        $this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    }
	private function config(){
		require_once("conexion.php");
		require_once("DataBase.php");
		$this->_db = new DataBase($SERVIDOR,$USUARIO,$PASSWORD,$BASE);
		mysql_query("SET NAMES utf8");
	}
}
?>